#ifndef __DEVNODE_H__
#define __DEVNODE_H__

#include <time.h>
#include <pthread.h>
#include <string.h>
#include <list.h>
#include <hydrology.h>

#define SPEAKER_POWER_PIN				5	//输出	GPIO0_A5_d	喇叭功放电源控制			1-使能电源/0-关闭电源
#define	ADAPTER_DETEC_PIN				146	//输入	GPIO4_C2_d	电源适配器插入检测			1-无插入/0-已插入

#define BATTERY_CHARGE_DETEC_PIN		122	//输入	GPIO3_D2_d	电池充电检测				1-电池充满电/0-电池在充电
#define BATTERY_CHARGING_PIN			123	//输出	GPIO3_D3_d	电池充电使能				1-使能充电/0-停止充电

#define DEVSN_LEN	15

#define SERVER_IP "183.6.105.178"  // RTU服务器IP地址
#define SERVER_PORT 10002       	// RTU服务器端口
//#define SERVER_IP "192.168.1.103"  // RTU服务器IP地址

//不能更改,跟报警器统一
typedef enum {
	ALARM_STATE_Null,						//无操作
	ALARM_STATE_Immediate_transfer, 		//立即转移
	ALARM_STATE_Prepare_transfer,			//准备转移
	ALARM_STATE_Caution,					//注意安全
	ALARM_STATE_Cancel_transfer, 			//取消警报
	ALARM_STATE_End,
	ALARM_STATE_Status, 					//在线点播	
} WARN_STATE_E;

typedef struct {
	//int dev_type;					//设备类型 呼叫器 报警器
	char dev_sn[32];				//终端设备码
	unsigned short online;			//终端在线状态	0-不在线	1-在线
	unsigned short online_lte;		//4G在线
	unsigned short online_tpunb;	//窄带在线
	unsigned short devaddr;			//终端窄带通信地址
	short dev_rssi;					//终端信号值	-127	~	-1
	unsigned short lte_csq;			//4G信号
	unsigned short ac_adapter;		//是否接入电源适配器
	unsigned short battery_level;	//终端电池电量
	unsigned short online_rsp;		//终端在线查询状态	0-不在线	1-在线	用在定时扫描时
	struct list_head node_list;		//终端设备链表

	unsigned short warn_rsp_state;	//1:报警未回复 2:报警已回复 3:报警已应答					终端回复
	unsigned int warn_rsp_time;		//报警回复		时间戳								终端回复

	unsigned int answer_rsp_time;	//已应答		时间戳								终端回复

	unsigned int cancel_rsp_state;	//1:消警未回复	2:消警已回复
	unsigned int cancel_rsp_time;	//消警回复		时间戳								终端回复
} DEV_NODE;


//一字节对齐
#pragma pack (1)

typedef struct {
	unsigned char remoter_addr[FRAME_REMOTER_ADDR_LEN];	//报警器SN
	unsigned int online_seq;							//指令序列号,用于去重处理
} __attribute__((aligned(1))) ONLINE_REQ_DATA;

/* 报警 */
typedef struct {
	unsigned char warn_status;							//报警类型		WARN_STATE_E
	unsigned int warn_seq;								//指令序列号,用于去重处理
	unsigned char remoter_addr[FRAME_REMOTER_ADDR_LEN];	//报警器SN
} __attribute__((aligned(1))) WARN_REQ_DATA;

typedef struct {
	unsigned char warn_status;							//报警类型		WARN_STATE_E
	unsigned char is_answer;							//是否已应答
} __attribute__((aligned(1))) WARN_RSP_DATA;


/* 语音广播 */

#define VOICE_BLOCK_SIZE	1024

#define	VOICE_FILE_NAME		"voice.file"

typedef struct {
	unsigned int voiceId;			//语音id		语音文件ID		id.voice	文件名
	unsigned short pack_num;		//包数		语音文件总包数
	unsigned short pack_seq;		//序号		分包序号		1 2 3 ...
	unsigned short pack_len;		//长度		压缩的数据长度
	unsigned char pack_data[1024];	//数据		压缩的数据
} __attribute__((aligned(1))) VOICE_REQ_DATA;

//补包
typedef struct {
	unsigned int voiceId;			//语音id		id.voice	文件名
	unsigned short seq_num; 		//丢包个数		1 2 3 ...
	unsigned short seq_array[0];	//序号数组
} __attribute__((aligned(1))) VOICE_RSP_DATA;


//广播任务
typedef struct {
	int broadcastTaskId;		//广播任务ID
	short broadcastType;		//广播类型 0:停止广播任务 1：图文消息 2:转移路线 3:注意安全 4:准备转移 5:立即转移 6:音频消息
	short command;				//1-更新 2-默认 3-播放 4-停止
	char md5sum[32];			//文件MD5
	char url[256];				//文件链接
} __attribute__((aligned(1))) BROADCASTTASK_REQ_DATA;

typedef struct {
	int broadcastTaskId;		//广播任务ID
	short broadcastStatus;		//广播任务状态
} __attribute__((aligned(1))) BROADCASTTASK_RSP_DATA;

#pragma pack ()

cJSON *cJSON_Create(char *command);
void gw_alarm_to_server(int alarm_level, double water_level);

DEV_NODE *get_dev_node(char *dev_sn);
DEV_NODE *get_dev_node_by_addr(unsigned short addr);

int devnode_manage_init(void);
void devNode_publish_all_to_server(void);
int sn_to_devid(const     char *dev_sn, unsigned char *devid);
void devid_to_sn(unsigned char *devid, char *dev_sn);

void sn_convert(char *to_devsn, char *from_devsn);

int get_signal(void);

//解析报警器数据
void update_devnode_info(DEV_NODE *dev_node);


//发布QT消息
struct qt_head_block {
	long msgtype;
	int cmdtype;
};

//需跟QT代码保持一致
struct qt_data_upblock {
	struct qt_head_block head;
	char dev_sn[24];				//设备sn
	int value;						//通用
	int water_level[3];				//三级水位阈值

	unsigned short online;			//在线状态
	unsigned short online_lte;		//4G在线状态
	unsigned short online_tpunb;	//窄带在线状态
	unsigned short devaddr;			//设备地址
	unsigned short battery_level;	//电池电量
	short dev_rssi;					//组网信号
	unsigned short lte_csq;			//公网信号
	unsigned short ac_adapter;		//用电状态

	//char gw_devSn[32];					//序列号
	//unsigned short gw_devAddr;			//地址
	unsigned char gw_devCellid;				//小区号
	unsigned short is_full_battery;			//满电状态
	//unsigned short battery_level;			//电池电量
	//unsigned short ac_adapter;			//适配器状态
	unsigned short mqtt_connect_status;		//MQTT状态
	unsigned short tpunb_ok;				//窄带通信状态
	char imei[32];							//IMEI
	char iccid[32];							//ICCID
	//unsigned short lte_csq;				//移动信号
	char ip[32];		   					//IP地址
	char version[32];						//版本号
};

//需跟QT代码保持一致
struct qt_data_downblock {
	struct qt_head_block head;
	int value;
	int command;	
	char devSn[32];
	int broadcast_type;	//广播任务使用
};

void online_state_notify_qt(char *dev_sn, int online);//通知qt桌面设备在线
void waring_state_notify_qt(char *dev_sn, int waring);//通知qt桌面设备已应答
void trigger_alarm_notify_qt(int alarm_level);	//1级报警	2级报警	3级报警
void water_level_notify_qt(int water_level);	//当前水位更新
void rain_level_notify_qt(int water_level);	//当前雨量更新
void warn_water_notify_qt(int water_level_1, int water_level_2, int water_level_3);	//三级水位阈值
void transferWaySetting_notify_qt(void);	//更新转移路线
void dev_status_notify_qt(DEV_NODE *dev_node);	//上报QT设备所有状态
void update_devnode_notify_qt(void);//MQTT下发更新终端信息
void broadcast_task_answer_update(int taskid);//更新QT广播任务应答数量


//设置报警器网络
int set_dev_node_netinfo(int gw_addr, char *dev_sn, int addr, int cellid);

//平台在线广播/点播
void dev_online_scan(char *dev_sn);

//平台报警广播/点播
void dev_warn_notify(char *dev_sn, int warn_level);

//平台消警广播/点播
void dev_warn_cancel_notify(char *dev_sn);


//报警状态处理逻辑接口
int update_warn_status(WARN_STATE_E alarm_value);
void warn_handler_start(void);

//QT下发报警点播\在线点播获取报警器所有状态
void warn_unicast_of_devSn(char *dev_sn, WARN_STATE_E warn_value);
void oline_unicast_of_devSn(char *dev_sn);

//QT界面查询所有状态
void get_all_status_info(void);

//QT发送广播任务
void *send_broadcast_task_thread(void *arg);
void broadcast_task_set_answer(int taskid, char *devSn);
void handle_broadcast_task(struct qt_data_downblock *data);
void start_broadcast_task(struct qt_data_downblock *data);

//终端信息
void update_devnode_from_file(char *filename);

//呼叫器水位报警值
int get_water_level_1(void);
int get_water_level_2(void);
int get_water_level_3(void);
int get_alarm_interval(void);
void water_alarm_info_parse(char *filename);


//获取在线状态线程
void stop_scan_online_thread(void);
void start_scan_online_thread(void);
void gw_heartBeat_to_server(void);

int get_battery_charge_state(void);

int get_battery_value(void);
int get_adapter_connect(void);

void set_backlight_brightness(int brightness);

#endif
